package org.pay.mch.user.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.pay.mch.common.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.entity.MchPassage;
import org.pay.core.entity.PayChannel;
import org.pay.mch.common.ctrl.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/02/11
 * @description:
 */
@RestController
@RequestMapping(Constant.MCH_CONTROLLER_ROOT_PATH + "/mch_passage")
@PreAuthorize("hasRole('"+ MchConstant.MCH_ROLE_NORMAL+"')")
public class MchPassageController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchPassage mchPassage = new MchPassage();
        mchPassage.setMchId(getUser().getId());
        int count = rpcCommonService.rpcMchPassageService.count(mchPassage);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchPassage> mchPassageList = rpcCommonService.rpcMchPassageService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), mchPassage);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchPassageList, count));
    }

    /**
     * 查询
     * @return
     */
    @RequestMapping("/pay_channel_list")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer passageId = getIntegerRequired(param, "passageId");
        PayChannel payChannel = new PayChannel();
        payChannel.setState(MchConstant.PUB_YES);
        payChannel.setPassageId(passageId);
        int count = rpcCommonService.rpcPayChannelService.count(payChannel);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<PayChannel> payChannelList = rpcCommonService.rpcPayChannelService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), payChannel);
        return ResponseEntity.ok(PayPageRes.buildSuccess(payChannelList, count));
    }

}
